/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.activity.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import link.chengguo.orangemall.api.OrangeException;
import link.chengguo.orangemall.common.service.impl.BaseServiceImpl;
import link.chengguo.orangemall.dozer.service.IGenerator;
import link.chengguo.orangemall.enums.OrderInfoEnum;
import link.chengguo.orangemall.modules.activity.domain.OrangeStoreBargain;
import link.chengguo.orangemall.modules.activity.domain.OrangeStoreBargainUser;
import link.chengguo.orangemall.modules.activity.domain.OrangeStoreBargainUserHelp;
import link.chengguo.orangemall.modules.activity.service.OrangeStoreBargainService;
import link.chengguo.orangemall.modules.activity.service.OrangeStoreBargainUserHelpService;
import link.chengguo.orangemall.modules.activity.service.OrangeStoreBargainUserService;
import link.chengguo.orangemall.modules.activity.service.mapper.OrangeStoreBargainUserMapper;
import link.chengguo.orangemall.modules.activity.vo.OrangeStoreBargainUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 * 用户参与砍价表 服务实现类
 * </p>
 *
 * @author chengguo
 * @since 2019-12-21
 */
@Slf4j
@Service
public class OrangeStoreBargainUserServiceImpl extends BaseServiceImpl<OrangeStoreBargainUserMapper, OrangeStoreBargainUser> implements OrangeStoreBargainUserService {


    @Autowired
    private IGenerator generator;

    @Autowired
    private OrangeStoreBargainUserMapper orangeStoreBargainUserMapper;

    @Autowired
    private OrangeStoreBargainService storeBargainService;
    @Autowired
    private OrangeStoreBargainUserHelpService storeBargainUserHelpService;


    /**
     * 修改用户砍价状态
     * @param bargainId 砍价产品id
     * @param uid 用户id
     */
    @Override
    public void setBargainUserStatus(Long bargainId, Long uid) {
        OrangeStoreBargainUser storeBargainUser = getBargainUserInfo(bargainId.longValue(),uid);
        if(ObjectUtil.isNull(storeBargainUser)) {
            return;
        }

        if(storeBargainUser.getStatus() != 1) {
            return;
        }
        double price = NumberUtil.sub(NumberUtil.sub(storeBargainUser.getBargainPrice(),
                storeBargainUser.getBargainPriceMin()),storeBargainUser.getPrice()).doubleValue();
        if(price > 0) {
            return;
        }

        storeBargainUser.setStatus(3);

        orangeStoreBargainUserMapper.updateById(storeBargainUser);
    }

    /**
     * 砍价取消
     * @param bargainId 砍价商品id
     * @param uid uid
     */
    @Override
    public void bargainCancel(Long bargainId, Long uid) {
        OrangeStoreBargainUser storeBargainUser = this.getBargainUserInfo(bargainId,uid);
        if(ObjectUtil.isNull(storeBargainUser)) {
            throw new OrangeException("数据不存在");
        }
        if(!OrderInfoEnum.BARGAIN_STATUS_1.getValue().equals(storeBargainUser.getStatus())){
            throw new OrangeException("状态错误");
        }
        orangeStoreBargainUserMapper.deleteById(storeBargainUser.getId());
    }

    /**
     * 获取用户的砍价产品
     * @param bargainUserUid 用户id
     * @param page page
     * @param limit limit
     * @return List
     */
    @Override
    public List<OrangeStoreBargainUserQueryVo> bargainUserList(Long bargainUserUid, int page, int limit) {
        Page<OrangeStoreBargainUser> pageModel = new Page<>(page, limit);
        return orangeStoreBargainUserMapper.getBargainUserList(bargainUserUid,pageModel);
    }

    /**
     * 判断用户是否还可以砍价
     * @param bargainId 砍价产品id
     * @param bargainUserUid 开启砍价用户id
     * @param uid  当前用户id
     * @return false=NO true=YES
     */
    @Override
    public  boolean isBargainUserHelp(Long bargainId, Long bargainUserUid, Long uid) {
        OrangeStoreBargainUser storeBargainUser = this.getBargainUserInfo(bargainId, bargainUserUid);
        OrangeStoreBargain storeBargain = storeBargainService
                .getById(bargainId);
        if(ObjectUtil.isNull(storeBargainUser) || ObjectUtil.isNull(storeBargain)){
            return false;
        }
        int count = storeBargainUserHelpService.lambdaQuery()
                .eq(OrangeStoreBargainUserHelp::getBargainId,bargainId)
                .eq(OrangeStoreBargainUserHelp::getBargainUserId,storeBargainUser.getId())
                .eq(OrangeStoreBargainUserHelp::getUid,uid)
                .count();
        if(count == 0) {
            return true;
        }
        return false;
    }

    /**
     * 添加砍价记录
     * @param bargainId 砍价商品id
     * @param uid 用户id
     */
    @Override
    public void setBargain(Long bargainId, Long uid) {
        OrangeStoreBargainUser storeBargainUser = this.getBargainUserInfo(bargainId,uid);
        if(storeBargainUser != null) {
            throw new OrangeException("你已经参与了");
        }
        OrangeStoreBargain storeBargain = storeBargainService.getById(bargainId);
        if(storeBargain == null) {
            throw new OrangeException("砍价商品不存在");
        }
        OrangeStoreBargainUser orangeStoreBargainUser = OrangeStoreBargainUser
                .builder()
                .bargainId(bargainId)
                .uid(uid)
                .bargainPrice(storeBargain.getPrice())
                .bargainPriceMin(storeBargain.getMinPrice())
                .price(BigDecimal.ZERO)
                .status(OrderInfoEnum.BARGAIN_STATUS_1.getValue())
                .build();
        orangeStoreBargainUserMapper.insert(orangeStoreBargainUser);
    }

//    /**
//     * 获取用户可以砍掉的价格
//     * @param id
//     * @return
//     */
//    @Override
//    public double getBargainUserDiffPrice(int id) {
//        OrangeStoreBargainUser storeBargainUserQueryVo = this.getById(id);
//        return NumberUtil.sub(storeBargainUserQueryVo.getBargainPrice()
//                ,storeBargainUserQueryVo.getBargainPriceMin()).doubleValue();
//    }



    /**
     * 获取某个用户参与砍价信息
     * @param bargainId 砍价id
     * @param uid 用户id
     * @return  OrangeStoreBargainUser
     */
    @Override
    public OrangeStoreBargainUser getBargainUserInfo(Long bargainId, Long uid) {
        QueryWrapper<OrangeStoreBargainUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(OrangeStoreBargainUser::getBargainId,bargainId)
                .eq(OrangeStoreBargainUser::getUid,uid)
                .last("limit 1");
        return orangeStoreBargainUserMapper.selectOne(wrapper);
    }

    /**
     * 获取参与砍价的用户数量
     * @param bargainId 砍价id
     * @param status  状态  OrderInfoEnum 1 进行中  2 结束失败  3结束成功
     * @return int
     */
    @Override
    public int getBargainUserCount(Long bargainId, Integer status) {
        return this.lambdaQuery().eq(OrangeStoreBargainUser::getBargainId,bargainId)
                .eq(OrangeStoreBargainUser::getStatus,status).count();
    }


//
//    /**
//     * 获取参与砍价的用户列表
//     * @param bargainId 砍价id
//     * @param status  状态  1 进行中  2 结束失败  3结束成功
//     * @return
//     */
//    @Override
//    public List<OrangeStoreBargainUserQueryVo> getBargainUserList(int bargainId, int status) {
//        QueryWrapper<OrangeStoreBargainUser> wrapper = new QueryWrapper<>();
//        wrapper.eq("bargain_id",bargainId).eq("status",status);
//        return generator.convert(orangeStoreBargainUserMapper.selectList(wrapper),
//                OrangeStoreBargainUserQueryVo.class);
//    }




}
